package chenjiuxu.seniorui.study;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by 15705 on 2017/12/25.
 * LinearGradient线性渲染
 */

public class LinearGradientView extends View {
    private final Paint paint;

    public LinearGradientView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int w = getWidth();
        int h = getHeight();
        int[] colors=new int[]{Color.BLACK,Color.RED,Color.WHITE,Color.BLUE};
        float[] positions= null;
        LinearGradient  linearGradient=new LinearGradient(0,h,w,h,colors,positions, Shader.TileMode.CLAMP);
        paint.setShader(linearGradient);
        canvas.drawRect(0,0,w,h,paint);
    }
}
